//
//  MSBaseScene.m
//  MagicSword
//
//  Created by Netease on 15/11/16.
//  Copyright © 2015年 Jonear. All rights reserved.
//

#import "MSBaseScene.h"
#import "MSTopBarNode.h"
#import "MSHeroScene.h"
#import "MSMainGameScene.h"
#import "MSHeroManager.h"
#import "MSWeaponManager.h"

@implementation MSBaseScene

- (void)didMoveToView:(SKView *)view {
    /* Setup your scene here */
    [super didMoveToView:view];

    if (!_topBarNode) {
        // 背景
        self.backgroundColor = [UIColor blackColor];
        SKSpriteNode *backgroundView = [SKSpriteNode spriteNodeWithImageNamed:@"main_background"];
        backgroundView.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
        backgroundView.size = self.size;
        backgroundView.alpha = 0.6;
        backgroundView.zPosition = 1;
        [self addChild:backgroundView];
        
        // topbar
        _topBarNode = [[MSTopBarNode alloc] init];
        _topBarNode.position = CGPointMake(self.frame.size.width/2, self.frame.size.height);
        _topBarNode.zPosition = 3;
        _topBarNode.name = NodeName_TopBarNode;
        [self addChild:_topBarNode];
    } else {
        [_topBarNode updateHeroArray];
    }
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint positionInScene = [touch locationInNode:self];
    SKSpriteNode *touchedNode = (SKSpriteNode *)[self nodeAtPoint:positionInScene];
    
    if ([[touchedNode name] isEqualToString:NodeName_TopBarNode]) {
        // 无
    }
    // 商店
    else if ([[touchedNode name] isEqualToString:NodeName_ShopNode]) {
        //
    }
    // 返回
    else if ([[touchedNode name] isEqualToString:NodeName_BackNode]) {
        //
    }
    // 选择英雄
    else if ([[touchedNode name] isEqualToString:NodeName_HeroNodes]) {
        //
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint positionInScene = [touch locationInNode:self];
    SKSpriteNode *touchedNode = (SKSpriteNode *)[self nodeAtPoint:positionInScene];
    
    if ([[touchedNode name] isEqualToString:NodeName_TopBarNode]) {
        // 无
    }
    // 商店
    else if ([[touchedNode name] isEqualToString:NodeName_ShopNode]) {
        
    }
    // 返回
    else if ([[touchedNode name] isEqualToString:NodeName_BackNode]) {
        [self.view popScene];
        // 保存数据
        [[MSHeroManager shareManager] saveHeroArray];
        [[MSWeaponManager shareManager] saveStoreWeapon];
    }
    // 选择英雄
    else if ([[touchedNode name] isEqualToString:NodeName_HeroNodes]) {
        if (![self isKindOfClass:[MSHeroScene class]]) {
            MSHeroScene *heroScene = [[MSHeroScene alloc] initWithSize:self.view.bounds.size];
            heroScene.scaleMode = SKSceneScaleModeAspectFill;
            [self.view pushScene:heroScene];
        }
    }
}


@end
